/*******************************************************************************
 Arie Emoticon Kaskus
 Copyright (c) 2008-2012 Ali Software


 Licences: MIT, GPL
 http://www.opensource.org/licenses/mit-license.php
 http://www.gnu.org/licenses/gpl.html
 ******************************************************************************/

/*
 * Name:arie.emoticons
 * Version: 1.0
 */


(function($) {
  jQuery.mbEmoticons= {
    author:"Arie",
    version:"1.0",
    smilesPath:"",
    smiles: {
      ":'(": "(",
":(": "(",
" :)b": ")b",
":)": ")",
":1thumbup": "1thumbup",
":2thumbup": "2thumbup",
";)": ";)",
":addfriends": "addfriends",
":afro:": "afro_",
":amazed:": "amazed_",
":angel:": "angel_",
":angel": "angel",
":anjing:": "anjing_",
":armys": "armys",
":army": "army",
":babi:": "babi_",
":babyboy1": "babyboy1",
":babygirl": "babygirl",
":babyboy": "babyboy",
":baby:": "baby_",
":batabig": "batabig",
":batas": "batas",
":bata": "bata",
":bedug": "bedug",
":beer:": "beer_",
":berbusas": "berbusas",
":berbusa": "berbusa",
":berdukas": "berdukas",
":berduka": "berduka",
":bigo:": "bigo_",
":bikini": "bikini_",
":bingungs": "bingungs",
":bingung:": "bingung_",
":bingung": "bingung",
":bola": "bola",
":bookmarks": "bookmarks",
":breakheart": "breakheart",
":buldog": "buldog",
":capedes": "capedes",
":cd:": "cd_",
":cd": "cd",
":cekpms": "cekpms",
":cekpm": "cekpm",
":cendolbig": "cendolbig",
":cendols": "cendols",
":cendolb": "cendolb",
":ck": "ck",
":clock": "clock",
":coffee:": "coffee_",
":confused": "confused",
":cool:": "cool_",
":cool": "cool",
":cystg": "cystg",
":D": "D",
":doctor": "doctor",
":dp": "dp",
":eek": "eek",
":email": "email",
":exclamati": "exclamati",
":flower:": "flower_",
":fm:": "fm_",
":frog:": "frog_",
":fuck2:": "fuck2_",
":fuck3:": "fuck3_",
":fuck:": "fuck_",
":games": "games",
":genit": "genit",
":gila:": "gila_",
":hammers": "hammers",
":hammer:": "hammer_",
":hammer": "hammer",
":heart:": "heart_",
":hi:": "hi_",
":hn": "hn",
":hoax": "hoax",
":iloveindonesias": "iloveindonesias",
":iloveindonesia": "iloveindonesia",
":ilovekaskuss": "iluvkaskuss",
":ilovekaskus": "ilovekaskus",
":jrb:": "jrb_",
":kacau:": "kacau_",
":kagets:": "kagets_",
":kbgt:": "kbgt_",
":ketupat": "ketupat",
":kimpoi:": "kimpoi_",
":kimpoi": "kimpoi",
":kissing:": "kissing_",
":kissmouth": "kissmouth",
":kisss": "kisss",
":kiss": "kiss",
":kr": "kr",
":kts:": "kts_",
":kucing:": "kucing_",
":linux1:": "linux1_",
":linux2:": "linux2_",
":maafaganwati": "maafaganwati",
":maafagan": "maafagan",
":mads": "mads",
":mad": "mad",
":mahos": "mahos",
":maho": "maho",
":malus": "malus",
":malu:": "malu_",
":malu": "malu",
":marah": "marah",
":matabelo:": "matabelo_",
":matabelo": "matabelo",
":medicine:": "medicine_",
":metal": "metal",
":mewek": "mewek",
":moon:": "moon_",
":najiss": "najiss",
":najis": "najis",
":ngacir2": "ngacir2",
":ngacir:": "ngacir_",
":ngacir": "ngacir",
":ngakaks": "ngakaks",
":ngakak": "ngakak",
":nohope:": "nohope_",
":nohope": "nohope",
":norose:": "norose_",
":nosara": "nosara",
":o": "o",
":Onigiri": "Onigiri",
":peluk": "peluk",
":Paws:": "Paws_",
":Peace:": "Peace_",
":Phone:": "Phone_",
":present:": "present_",
":p": "p",
":rainbow:": "rainbow_",
":rain:": "rain_",
":recsel": "recsel",
":repost2": "repost2",
":repost:": "repost3",
":reposts": "reposts",
":repost": "repost",
":request": "request",
":ricebowl:": "ricebowl_",
":rolleyes": "rolleyes",
":rose:": "rose_",
":salahkamar": "salahkamar",
":salaman": "salaman",
":selamat": "selamat",
":shakehand2": "shakehand2",
":shakehand": "shakehand",
":shutups": "shutups",
":shutup:": "shutup_",
":siul": "siul",
":sorry": "sorry",
":sup:": "sup_",
":sundulgans": "sundulgans",
":sun:": "sun_",
":sup2": "sup2",
":table:": "table_",
":tabrakan:": "tabrakan_",
":tai": "tai",
":takuts": "takuts",
":takut": "takut",
":think:": "think_",
":thumbdown": "thumbdown",
":thumbup": "thumbup",
":toast": "toast",
":travel": "traveller",
":tv": "tv",
":ultah": "ultah",
":wowcantik": "wowcantik",
":rate5": "rate5",
":Yb": "Yb"
    },
    smilesVariations: {
      ":-)": "smile",
      ":)": "smile"
    },
    smileBoxBtn:"#smileBoxBtn"
    ,
    getRegExp:function(){
      var ret="/";
      $.each($.mbEmoticons.smilesVariations,function(i){
        var emot= i.replace(/\)/g,"\\)").replace(/\(/g, "\\(").replace(/\|/g, "\\|").replace(/\*/g, "\\*").replace(/\^/g, "\\^");
        ret +="("+emot+")|";
      });
      ret+="/g";
      return eval(ret);
    },
    addSmilesBox:function(){
      $(this).each(function(){

        var textarea=$(this);

        var wrapper=$("<span/>").addClass("mbSmilesWrapper");
        textarea.wrapAll(wrapper);

        textarea.data("caret",textarea.caret());
        textarea.data("smilesIsOpen",true);
        var smilesBox=$("<div/>").addClass("mbSmilesBox").hide();
        var smilesButton=$("<span/>").addClass("mbSmilesButton").html(":-)").emoticonize();
        $.each($.mbEmoticons.smiles,function(i){
          var emoticon=$("<span/>").addClass("emoticon").html(i).attr("title",i);
          smilesBox.append(emoticon);
          emoticon.emoticonize().data("emoticon",i);
        });
        textarea.before(smilesButton);
        smilesButton.click(function(){
          textarea.mbOpenSmilesBox();
        });
        textarea.before(smilesBox);
        textarea.data("smilesBox",smilesBox);

        smilesBox.find(".emoticon").each(function(){
          var icon=$(this);
          $(this).hover(
                  function(){
                    var src= $(this).find("img").attr("src").replace(".png",".gif");
                    $(this).find("img").attr("src",src);
                  },
                  function(){
                    var src= $(this).find("img").attr("src").replace(".gif",".png");
                    $(this).find("img").attr("src",src);
                  })
                  .click(function(){
            textarea.insertAtCaret(" "+icon.data("emoticon")+" ");
          });
        });
        textarea.focus();
        textarea.caret(textarea.data("caret"));
      });
      return this;
    },

    openSmileBox:function(){
      var textarea = $(this);
      var smilesBox= textarea.data("smilesBox");
      var left= (textarea.position().left+(textarea.outerWidth()/2));
      smilesBox.css({left:left});
      smilesBox.fadeIn();
      setTimeout(function(){
        $(document).one("click",function(){textarea.mbCloseSmilesBox();});
      },100);
    },

    removeSmilesBox:function(){
      $(this).data("smilesIsOpen",false);
      var smilesBox= $(this).data("smilesBox");
      smilesBox.fadeOut(500);
    }
  };

  jQuery.fn.insertAtCaret = function (tagName) {
    return this.each(function(){
      if (document.selection) {
        //IE support
        this.focus();
        sel = document.selection.createRange();
        sel.text = tagName;
        this.focus();
      }else if (this.selectionStart || this.selectionStart == '0') {
        //MOZILLA/NETSCAPE support
        startPos = this.selectionStart;
        endPos = this.selectionEnd;
        scrollTop = this.scrollTop;
        this.value = this.value.substring(0, startPos) + tagName + this.value.substring(endPos,this.value.length);
        this.focus();
        this.selectionStart = startPos + tagName.length;
        this.selectionEnd = startPos + tagName.length;
        this.scrollTop = scrollTop;
      } else {
        this.value += tagName;
        this.focus();
      }
    });
  };

  jQuery.fn.caret = function(pos) {
    var target = this[0];
    if (arguments.length == 0) { //get
      if (target.selectionStart) { //DOM
        var pos = target.selectionStart;
        return pos > 0 ? pos : 0;
      }
      else if (target.createTextRange) { //IE
        target.focus();
        var range = document.selection.createRange();
        if (range == null)
          return '0';
        var re = target.createTextRange();
        var rc = re.duplicate();
        re.moveToBookmark(range.getBookmark());
        rc.setEndPoint('EndToStart', re);
        return rc.text.length;
      }
      else return 0;
    } //set
    if (target.setSelectionRange) //DOM
      target.setSelectionRange(pos, pos);
    else if (target.createTextRange) { //IE
      var range = target.createTextRange();
      range.collapse(true);
      range.moveEnd('character', pos);
      range.moveStart('character', pos);
      range.select();
    }
  };

  // variation from Roberto Bichierai emoticonize component.
  jQuery.fn.emoticonize = function (animate) {
    function convert (text){    
       //alert();
    //  if ($('pre')[0].tagName == ''){
        
      var icons = $.mbEmoticons.getRegExp();
      return text.replace (icons,function(str){
        var ret= $.mbEmoticons.smilesVariations[str];
        var ext=animate?".gif":".png";
        if (ret){
          ret="<img src='"+$.mbEmoticons.smilesPath+"https://arietux.googlecode.com/svn/trunk/smiley/"+ret+ext+"' align='absmiddle'>";
          return ret;
        }else{
          return str;
        }
      });
    
    //}else{return text.val();}
    }
    this.each(function() {
      var el = $(this);
         
        var html = convert(el.html());
      
      el.html(html);
    });
    return this;
  };

  $.extend($.mbEmoticons.smilesVariations,$.mbEmoticons.smiles);

  jQuery.fn.mbSmilesBox= $.mbEmoticons.addSmilesBox;
  jQuery.fn.mbOpenSmilesBox= $.mbEmoticons.openSmileBox;
  jQuery.fn.mbCloseSmilesBox= $.mbEmoticons.removeSmilesBox;

})(jQuery);
